package longshu.data.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import longshu.data.common.RetV;
import longshu.data.common.ds.DataSourceKey;
import longshu.data.common.ds.DynamicDataSource;
import longshu.data.domain.Student;
import longshu.data.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * StudentController
 *
 * @author LongShu 2018-02-03
 */
@Controller
@RequestMapping("/student")
public class StudentController extends BaseController<StudentService> {

    @RequestMapping("/list")
    @ResponseBody
    public Object list(Student student,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "20") Integer PageSize,
                       @RequestParam(defaultValue = DataSourceKey.SQLITE) String ds) {

        // 切换数据源
        DynamicDataSource.setDataSourceKey(ds);
        // 分页
        PageHelper.startPage(pageNum, PageSize);

        List<Student> studentList = service.select(student);
        logger.info("studentList:{}", studentList);
        PageInfo<Student> pageInfo = new PageInfo<>(studentList);

        return RetV.ok(pageInfo);
    }

}
